package org.common.utils.fasttest;

import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONWriter;
import com.alibaba.fastjson2.TypeReference;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.common.utils.fasttest.entity.RoleResourceModel;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author zhouzhibing
 * @date 2024/9/9
 */
public class TestJson {

    private static final Map<Integer , RoleResourceModel> MODEL_MAP;

    private static final String JSON_VALUE;

    private static final int TEST_COUNT = 100_0000;

    static  {
        MODEL_MAP = new HashMap<>();
        for (int i = 0; i < 10; i++) {
            RoleResourceModel model = new RoleResourceModel();
            model.setRoleId(i);
            model.setDiamond("2");
            model.setGold(3);
            MODEL_MAP.put( i , model);
        }
        JSON_VALUE = JSONObject.toJSONString(MODEL_MAP);
    }
    public static void test() throws Exception {
//        testFastJson();
//        testJackJson();
//        testNonDefault();
    }

    private static void testNonDefault() throws Exception {
        TestJsonObject object = new TestJsonObject();


        String jsonString = JSONObject.toJSONString(object, JSONWriter.Feature.NotWriteDefaultValue);
        ObjectMapper mapper = new ObjectMapper();
        String jsonString1 = mapper.writeValueAsString(object);

        System.out.println("fastjson = " + jsonString);
        System.out.println("jackson  = " + jsonString1);

        TestJsonObject object1 = JSONObject.parseObject(jsonString, TestJsonObject.class);
        System.out.println(object1);
    }

    private static void testFastJson() {
//        Test.doHandler("JSONObject.toJSONString" , TEST_COUNT , 10 , (index) -> {
//            JSONObject.toJSONString(JSON_VALUE);
//        });

        Test.doHandler("JSONObject.parseObject" , TEST_COUNT , 10 , (index) -> {
            Map<Integer , RoleResourceModel> map = JSONObject.parseObject(JSON_VALUE, new TypeReference<>() {});
        });
    }

    private static void testJackJson() throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        Test.doHandler("jackson.writeValueAsString" , TEST_COUNT , 10 , (index) -> {
            try {
                mapper.writeValueAsString(MODEL_MAP);
            } catch (JsonProcessingException e) {
                throw new RuntimeException(e);
            }
        });
    }


    @JsonInclude(JsonInclude.Include.NON_DEFAULT)
    private static class TestJsonObject {
        public int number;

        public long number1;

        public float number2;

        public double number3;

        public String string1;

        public boolean boolean1;

        public RoleResourceModel model = new RoleResourceModel();

        public Map<String , String> map = new ConcurrentHashMap<>();
    }

}
